/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.
  
  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.smartthinking.qualitystores;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

public class EditorFragment extends  Activity {
  private TextView txtResultado, txtsaldo, txtliqui, txtmontovenletra, txtmontoven, txtpagomin, txtpago, txtlineadisart, txtlineadispres;

  @Override
  public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.editor);
	  getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    
    txtResultado = (TextView)findViewById(R.id.txtResultado);
    txtsaldo = (TextView) findViewById(R.id.txtSALDOtxt);
    txtliqui = (TextView) findViewById(R.id.txtLIQUIDACIONtxt);
    txtmontovenletra = (TextView) findViewById(R.id.txtMONTOVENCIDOLETRAtxt);
    txtmontoven = (TextView) findViewById(R.id.txtMONTOVENCIDOtxt);
    txtpagomin = (TextView) findViewById(R.id.txtPAGOMINIMOtxt);
    txtpago = (TextView) findViewById(R.id.txtPAGOtxt);
    txtlineadisart = (TextView) findViewById(R.id.txtLINEADIPONIBLEARTtxt);
    txtlineadispres = (TextView) findViewById(R.id.txtLINEADISPONIBLEPREStxt);
    
    
    TareaWSInsercion1 tarea = new TareaWSInsercion1();
    tarea.execute();

  }
  
  private class TareaWSInsercion1 extends AsyncTask<String,Integer,Boolean> {
		
//  	private Cliente[] listaClientes;
  	
	    protected Boolean doInBackground(String... params) {
	    	
	    	boolean resul = true;
	 
	    	final String SOAP_ACTION = "http://www.qualitystores.com.mx/WebServiceEstadoCuenta/servicioCel2.php#EdoCta";
	    	final String NAMESPACE = "http://www.qualitystores.com.mx/WebServiceEstadoCuenta/servicioCel2.php";
	    	final String METHOD_NAME = "getEdoCta";
	    	final String URL = "http://www.qualitystores.com.mx/WebServiceEstadoCuenta/servicioCel2.php?wsdl";

	    	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

	    	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.dotNet = true;

			envelope.setOutputSoapObject(request);

//			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			try 
			{
				HttpTransportSE transporte = new HttpTransportSE(URL);
				Log.i("entro al try", "entro al try");
				transporte.call(SOAP_ACTION, envelope);
				Log.i("entro al try", "transporte"+transporte);
//				SoapObject resSoap =(SoapObject)envelope.getResponse();
				
				SoapObject obj1 = (SoapObject) envelope.bodyIn;
				Log.i("entro al try", "obj1"+obj1);
				SoapObject obj2 =(SoapObject) obj1.getProperty(0);
				
				Log.i("entro al try", "obj2"+obj2);
//				listaClientes = new Cliente[resSoap.getPropertyCount()];
				for(int i=0; i<obj2.getPropertyCount(); i++)
				{
					Log.i("entro al for", "entro al for");
					SoapObject obj3 =(SoapObject) obj2.getProperty(0);
//					SoapPrimitive ic = (SoapPrimitive)response.getProperty(0);
					 Log.i("entro al for", "entro al for"+obj3);
			           Cliente cli = new Cliente();
//				       cli.ultupdt = ic.getProperty(0).toString();
//				       cli.almacen = ic.getProperty(1).toString();
//				       cli.monto = ic.getProperty(2).toString();
//				       cli.tipo = ic.getProperty(3).toString();
				       cli.saldo_actual = Float.parseFloat(obj2.getProperty(0).toString());
				       float liquede_con = Float.parseFloat(obj2.getProperty(1).toString());
				       float mv_la = Float.parseFloat(obj2.getProperty(2).toString());
				       float mv = Float.parseFloat(obj2.getProperty(3).toString());
				       float pm24 = Float.parseFloat(obj2.getProperty(4).toString());
				       float pm12 = Float.parseFloat(obj2.getProperty(5).toString());
				       float linea_articulos = Float.parseFloat(obj2.getProperty(6).toString());
				       float linea_prestamos = Float.parseFloat(obj2.getProperty(7).toString());
				       Log.i("saldo actual", ""+cli.saldo_actual);
//				       txtResultado.setText(String.valueOf(cli.saldo_actual));
				       txtsaldo.setText(String.valueOf(cli.saldo_actual));
				       txtliqui.setText(String.valueOf(liquede_con));
				       txtmontovenletra.setText(String.valueOf(mv_la));
				       txtmontoven.setText(String.valueOf(mv));
				       txtpagomin.setText(String.valueOf(pm24));
				       txtpago.setText(String.valueOf(pm12));
				       txtlineadisart.setText(String.valueOf(linea_articulos));
				       txtlineadispres.setText(String.valueOf(linea_prestamos));
				       
				}
				
			          
 				       
			} 
			catch (Exception e) 
			{
				resul = false;
			} 
			Log.i("RESULT listado doinback", ""+resul);
	        return resul;
	    }
	    
	    protected void onPostExecute(Boolean result) {
	    	Log.i("RESULT listado", ""+result);
	    	
	    	
	    	
	    	if (result)
	    	{
		    	//Rellenamos la lista con los nombres de los clientes
//				final String[] datos = new String[listaClientes.length];
//				 
//				for(int i=0; i<listaClientes.length; i++)
//					 datos[i] = listaClientes[i].ultupdt;
//				
//				ArrayAdapter<String> adaptador =
//				    new ArrayAdapter<String>(getActivity(),
//				        android.R.layout.simple_list_item_1, datos);
//				 
//				list.setAdapter(adaptador);
	    		txtResultado.setText("Insertado OK");
	    	}
	    	else
	    	{
	    		txtResultado.setText("Error!");
	    	}
	    }
	}
  
}